package com.space.school.controller;

import com.space.school.domain.User;
import com.space.school.service.UserService;
import com.space.school.service.impl.UserServiceImpl;
import com.space.school.utils.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


@WebServlet(name = "userServlet",value = "/userServlet")
public class UserServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        设置编码格式
        req.setCharacterEncoding("UTF-8");
//        获取当前页数
        String currentPage=req.getParameter("currentPage");
//        如果第一次进页面,当前页数为空或为null
//        如果进页面的时候PB没有传进去,所以获取不到值就为null
        if(currentPage==null || currentPage.equals("")) {
            currentPage="1";
        }
        int pageSize=3;
        Map<String,String[]>map=req.getParameterMap();
        PageBean<User> pb=userService.search(map,Integer.parseInt(currentPage), pageSize);
//        将查询结果放入集合
        req.setAttribute("PB",pb);
        req.setAttribute("userName",req.getParameter("userName"));
        req.setAttribute("status",req.getParameter("status"));
        req.getRequestDispatcher("userList.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        搜索条件
//        1.
//        String userName=req.getParameter("userName");
//        String status=req.getParameter("status");
//        2.
//        取查询的条件\
        req.setCharacterEncoding("UTF-8");


//        取Cookie
//        Cookie[] cookies=req.getCookies();

//        取session (有参数)
//        HttpSession session=req.getSession(false);
//        if(session!=null) {
//            String userName=session.getAttribute("userName").toString();
//        }




        Map<String,String[]>map=req.getParameterMap();
        String currentPage=req.getParameter("currentPage");
        if(currentPage==null || currentPage.equals("")) {
            currentPage="1";
        }
        int pageSize=3;
        PageBean<User>userList = userService.search(map, Integer.parseInt(currentPage),pageSize);
        req.setAttribute("PB",userList);
        req.setAttribute("userName",req.getParameter("userName"));
        req.setAttribute("status",req.getParameter("status"));
        req.getRequestDispatcher("userList.jsp").forward(req,resp);

    }
}
